package com.erp.produce.service.impl;

import com.erp.common.util.TimeUtil;
import com.erp.stock.dao.FinishedGoodsInventoryDao;
import com.erp.produce.dao.ProductionTaskPrintingDyeingDao;
import com.erp.produce.entity.ProductionTaskPrintingDyeing;
import com.erp.produce.service.FinishedGoodsInventoryService;
import com.erp.produce.service.OrderService;
import com.erp.produce.service.ProductionTaskPrintingDyeingService;
import com.erp.stock.entity.FinishedGoodsInventory;
import com.erp.stock.vo.ProductionTaskPrintAndDyeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ProductionTaskPrintingDyeingServiceImpl implements ProductionTaskPrintingDyeingService {
    @Autowired
    ProductionTaskPrintingDyeingDao dao;

    @Autowired
    OrderService orderService;

    @Autowired
    FinishedGoodsInventoryService finishedGoodsInventoryService;

    @Autowired
    FinishedGoodsInventoryDao finishedGoodsInventoryDao;

    @Transactional
    @Override
    public Boolean savePrintingDyeingTasks(ProductionTaskPrintAndDyeVO[] list) {
        Long orderId = 0L;
        if (list.length>0) {
            orderId = list[0].getOrderId();
        }
        try {
            for (ProductionTaskPrintAndDyeVO vo : list) {

                ProductionTaskPrintingDyeing entity = new ProductionTaskPrintingDyeing();
                entity.setCount(vo.getCount());
                entity.setCreateTime(TimeUtil.now());
                entity.setGoodsId(vo.getProcessedGoodsId());
                entity.setGoodsName(vo.getProcessedGoodsName());
                entity.setOrderId(vo.getOrderId());
                entity.setPrintAndDyeEnterpriseName(vo.getPrintAndDyeEnterpriseName());
                entity.setTaskStatus(0);
                entity.setUnit(vo.getUnit());
                dao.save(entity);
            }
            //改变订单状态
            if (orderId != 0) {
                orderService.changeStatus(6,orderId);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public List<ProductionTaskPrintingDyeing> list(Long orderId) {
        return dao.findAllByOrderId(orderId);
    }

    @Override
    @Transactional
    public Boolean saveList(ProductionTaskPrintingDyeing[] list) {
        Boolean allSuccess = true;
        List<FinishedGoodsInventory> finishedGoodsInventories = new ArrayList<>();
        for (ProductionTaskPrintingDyeing entity : list) {
            System.out.println(entity);
            //若实体对象为
            if (entity.getTaskStatus() == 0) {
                allSuccess = false;
            } else {
                FinishedGoodsInventory finishedGoodsInventory =
                        finishedGoodsInventoryDao.findFirstByGoodsIdAndOrderId(entity.getGoodsId(),entity.getOrderId());
                System.out.println(finishedGoodsInventory == null);
                if (finishedGoodsInventory == null) {
                    finishedGoodsInventory = new FinishedGoodsInventory();
                    finishedGoodsInventory.setGoodsId(entity.getGoodsId());
                    finishedGoodsInventory.setGoodsName(entity.getGoodsName());
                    finishedGoodsInventory.setOrderId(entity.getOrderId());
                    finishedGoodsInventory.setInventory(entity.getCount());
                    finishedGoodsInventory.setUnit(entity.getUnit());
                    finishedGoodsInventory.setPrintAndDyeEnterpriseName(entity.getPrintAndDyeEnterpriseName());
                    finishedGoodsInventory.setInOfStockTime(TimeUtil.now());
                    finishedGoodsInventories.add(finishedGoodsInventory);
                }
            }
        }
        //将状态为1的保存到成品库存中
        if (finishedGoodsInventories.size() > 0) {
            finishedGoodsInventoryService.saveList(finishedGoodsInventories);
        }
        //若全部印染任务完成，改变订单状态为7
        if (allSuccess) {
            orderService.changeStatus(7,list[0].getOrderId());
        }
        return dao.saveAll(Arrays.asList(list)) != null;
    }
}
